import { MailDefine } from "../../../kds-base-define/src/MailDefine";
import { Rpc } from "../rpc";
import { kdutils } from "kdweb-core/lib/utils";
import { DB } from "../db";
import { ServerValues } from "../../../kds-base-define/src/ServerConfig";
import { RpcUserFlag } from "./user";
import { UserFlag } from "../../../kds-base-define/src/UserFlag";

let db = DB.get(ServerValues.dbLobby,"maildb")
let mailTablename = "t_mails"

let redis = DB.redis
let newMailTablename = "t_newmail"
export type UserMailPOData = {
	userID:number,
	mails:MailDefine.MailData[],
}

async function getMailID(userID:number) {
	let id:number = await Rpc.center.callException("kds.ids.getId","user-mail",1000000,1)
	if(id == null) {
		return null
	}
	return userID.toString() + "-" + id
}

function getUserIDByMailID(mailID:string) {
	let arr = mailID.split("-")
	return Number.parseInt(arr[0])
}
async function getMails(h:string,userID:number) {
	let poData:UserMailPOData = await db.getSingle(mailTablename,{userID:userID})
	if(poData == null) {
		return []
	}
	return poData.mails
}

async function getMail(h:string,mailID:string) {
	let userID = getUserIDByMailID(mailID)
	let poData:UserMailPOData = await db.getSingle(mailTablename,{userID:userID})
	if(poData == null) {
		return null
	}
	return poData.mails.find((v)=>v.mailID == mailID)
}

async function addMail(h:string,userID:number,mail:MailDefine.MailData) {
	mail.mailID = await getMailID(userID)
	if(mail.mailID == null){
		return null
	}
	mail.userID = userID
	mail.read = false 
	mail.timestamp = kdutils.getMillionSecond()
	mail.date = kdutils.getFmtMoment("YYYY-MM-DD HH:mm:ss")
	let poData:UserMailPOData = await db.getSingle(mailTablename,{userID:userID})
	let insert = false 
	if(poData == null) {
		poData = {
			userID:userID,
			mails:[]
		}
		insert = true 
	}

	poData.mails.push(mail)
	if(insert) {
		await db.insert(mailTablename,poData)
	} else {
		await db.update(mailTablename,{userID:userID},poData)
	}
	await setNewMail(h,userID)
	return mail
}

async function readMail(h:string,mailID:string) {
	let userID = getUserIDByMailID(mailID)
	let poData:UserMailPOData = await db.getSingle(mailTablename,{userID:userID})
	if(poData == null) {
		return false
	}
	let mail = poData.mails.find((v)=>v.mailID == mailID)
	if(mail == null) {
		return false
	}
	mail.read = true 
	await db.update(mailTablename,{userID:userID},poData)
	return true 
}

async function deleteMail(h:string,mailID:string) {
	let userID = getUserIDByMailID(mailID)
	let poData:UserMailPOData = await db.getSingle(mailTablename,{userID:userID})
	if(poData == null) {
		return null
	}
	let mailIdx = poData.mails.findIndex((v)=>v.mailID == mailID)
	if(mailIdx < 0) {
		return null 
	}
	let mail = poData.mails[mailIdx]
	poData.mails.splice(mailIdx,1)
	await db.update(mailTablename,{userID:userID},poData)
	return mail 
}

export let RpcMail = {
	getAll:getMails,
	get:getMail,
	add:addMail,
	read:readMail,
	delete:deleteMail,
}

async function setNewMail(h:string,userID:number) {
	await RpcUserFlag.set(h,userID,UserFlag.NewMail,true)
	return true 
}

async function getNewMail(h:string,userID:number) {
	return await RpcUserFlag.get(h,userID,UserFlag.NewMail)
}
async function removeNewMail(h:string,userID:number) {
	await RpcUserFlag.remove(h,userID,UserFlag.NewMail)
	return true 
}

export let RpcNewMail = {
	set:setNewMail,
	get:getNewMail,
	remove:removeNewMail,
}